package com.group4.edu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group4.edu.entity.Teacher;
import com.group4.edu.service.TeacherService;
import com.study.util.LogUtils;
import com.study.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("list")
    public ResponseResult getAll(){
        LogUtils.info(log,"查所有老师");
        List<Teacher> list = teacherService.list(null);
        return new ResponseResult(list);
    }

    @GetMapping("listbypage/{pageNum}")
    public ResponseResult<PageInfo<Teacher>> listAll(@PathVariable int pageNum) {
        LogUtils.info(log,"分页查所有老师");
        PageHelper.startPage(pageNum, 8);
        PageInfo<Teacher> teachers = teacherService.listAll(pageNum);
        return new ResponseResult<>(teachers);
    }

    @GetMapping("listbycondition/{pageNum}")
    public ResponseResult<PageInfo<Teacher>> listByCondition(@RequestBody Teacher teacher, @PathVariable int pageNum) {
        PageHelper.startPage(pageNum, 8);
        PageInfo<Teacher> teachers = teacherService.listByCondition(pageNum, teacher);
        return new ResponseResult<>(teachers);
    }

    @PostMapping("update")
    @ResponseBody
    public ResponseResult<Void> update(@RequestBody Teacher teacher) {
        teacherService.update(teacher);
        return ResponseResult.SUCCESS;
    }
    @GetMapping("findteacher")
    public ResponseResult findTeacher(@RequestBody Teacher teacher) {
        List<Teacher> teachers = teacherService.findBy(teacher);
        return new ResponseResult<>(teachers);
    }
    @PostMapping("del")
    @ResponseBody
    public ResponseResult<Void> del(@RequestBody Teacher teacher) {
        teacherService.update(teacher);
        return ResponseResult.SUCCESS;
    }
    @PostMapping("add")
    @ResponseBody
    public ResponseResult<Void> add(@RequestBody Teacher teacher){
        teacherService.save(teacher);
        return ResponseResult.SUCCESS;
    }
    @GetMapping("getTeacher/{id}")
    public ResponseResult<Teacher> getTeacher(@PathVariable String id) {
        Teacher byId = teacherService.getById(id);
        return new ResponseResult<>(byId);
    }
}

